using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;

namespace ReportMaintenanceUtility
{
	public class Query : GenericBase<Query>
	{
		#region PRIVATE MEMBERS
		private QueryType				_seed		= null;
		private string					_cmdText	= string.Empty;
		private string					_dsName		= string.Empty;
		private QueryTypeCommandType	_qtct		= QueryTypeCommandType.StoredProcedure;
		#endregion

		#region PUBLIC PROPERTIES

		public string CommandText
		{
			get { return this._cmdText; }
			set
			{
				this.PrepStateChange("CommandText");
				this._cmdText = value;
			}
		}

		public string DataSourceName
		{
			get { return this._dsName; }
			set
			{
				this.PrepStateChange("DataSourceName");
				this._dsName = value;
			}
		}

		public QueryTypeCommandType CommandType
		{
			get { return this._qtct; }
			set
			{
				this.PrepStateChange("CommandType");
				this._qtct = value;
			}
		}
		#endregion

		#region CONSTRUCTOR(S)

		private Query(Query q)
		{
			if (q == null)
			{
				throw new ArgumentNullException();
			}
			else
			{
				this._seed = (QueryType)q.CreateSeedCopy();
				base.CopyListeners(q);
				this.Initialize();
			}
		}

		public Query(QueryType qt)
		{
			if (qt == null)
			{
				throw new ArgumentNullException();
			}
			else
			{
				this._seed = qt;
				this.Initialize();
			}
		}
		protected override void Initialize()
		{
			for (int i = 0; i < this._seed.ItemsElementName.Length; i++)
			{
				switch (this._seed.ItemsElementName[i])
				{
					case ItemsChoiceType2.CommandText:
						this._cmdText = this._seed.Items[i].ToString();
						break;
					case ItemsChoiceType2.CommandType:
						this._qtct = (QueryTypeCommandType)this._seed.Items[i];
						break;
					case ItemsChoiceType2.DataSourceName:
						this._dsName = this._seed.Items[i].ToString();
						break;
				}
			}
		}
		#endregion

		#region	INTERFACE IMPLEMENATION(S)

		public override bool EqualTo(Query other)
		{
			if (other == null)
			{
				return false;
			}
			else
			{
				if (this.CommandType == other.CommandType &&
					this.CommandText.Equals(other.CommandText, StringComparison.CurrentCultureIgnoreCase) &&
					this.DataSourceName.Equals(other.DataSourceName, StringComparison.CurrentCultureIgnoreCase))
				{
					return true;
				}
				else
				{
					return false;
				}
			}
		}

		public override List<ChangeLog> SaveState()
		{
			List<ChangeLog> log = new List<ChangeLog>();

			if (this._isDirty)
			{
				//List<ItemsChoiceType2> elements = new List<ItemsChoiceType2>(this._seed.ItemsElementName);
				//List<object> items = new List<object>(this._seed.Items);

				//if (!this._cmdText.Equals(this._copy._cmdText, StringComparison.CurrentCultureIgnoreCase))
				//{
				//    int idx = elements.IndexOf(ItemsChoiceType2.CommandText);

				//    log.Add(new ChangeLog(ChangeType.QueryCommandText, this._copy._cmdText, this._cmdText));
				//    //this._seed.Items[i] = this._cmdText;
				//}
				//break;

				//if (((QueryTypeCommandType)this._seed.Items[i]) != this._qtct)
				//{
				//    log.Add(new ChangeLog(ChangeType.QueryCommandType, this._seed.Items[i], this._qtct));
				//    //this._seed.Items[i] = this._qtct;
				//}
				//break;

				//if (!this._seed.Items[i].ToString().Equals(this._dsName, StringComparison.CurrentCultureIgnoreCase))
				//{
				//    log.Add(new ChangeLog(ChangeType.DataSourceName, this._seed.Items[i], this._dsName));
				//    this._seed.Items[i] = this._dsName;
				//}
				//break;
			}

			this.ClearState();

			return log;
		}

		#endregion

		public override void RestoreState()
		{
			if (this._copy != null)
			{
				this._cmdText = this._copy._cmdText;
				this._dsName = this._copy._dsName;
				this._qtct = this._copy._qtct;
			}
			this.ClearState();
		}

		public override Query CreateDeepCopy()
		{
			if (this._copy != null)
			{
				return this._copy;
			}
			return new Query(this);
		}

		public override object CreateSeedCopy()
		{
			QueryType qt = new QueryType();
			List<ItemsChoiceType2> elements = new List<ItemsChoiceType2>(this._seed.ItemsElementName);
			List<object> items = new List<object>(this._seed.Items);

			qt.ItemsElementName = elements.ToArray();
			qt.Items = items.ToArray();

			return qt;
		}

		public object GetSeedObject()
		{
			throw new Exception("The method or operation is not implemented.");
		}
	}
}
